/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_atomic.h
 *
 * Purpose: implementation atomic operation
 *
 * Developer:
 *   wen.gu , 2022-03-20
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __CBUS_ATOMIC_H__
#define __CBUS_ATOMIC_H__
#include "cbus/core/cbus_type.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START

typedef GS32 volatile cbus_atomic_int;
typedef GS64 volatile cbus_atomic_int64;

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
/**
 *
 *@brief  add a value to org_value as atomic operation
 *
 *@param  org_value [io] origin value.
 *@param  add_value [in] add value.
 *
 *@return previous org_value.
 *
 *@see    
 */
 G_API cbus_atomic_int cbus_atomic_exchange_add(cbus_atomic_int *org_value, cbus_atomic_int add_value);

/**
 *
 *@brief  add 1 to org_value as atomic operation
 *
 *@param  org_value [io] origin value.
 *
 *@return previous org_value.
 *
 *@see
 */
G_API cbus_atomic_int cbus_atomic_increment(cbus_atomic_int *org_value);

/**
 *
 *@brief  dec  1 from org_value as atomic operation
 *
 *@param  org_value [io] origin value.
 *
 *@return previous org_value.
 *
 *@see
 */
G_API cbus_atomic_int cbus_atomic_decrement(cbus_atomic_int *org_value);

/**
 *
 *@brief  atomic 'or' operation
 *
 *@param  org_value [io] origin value.
 *
 *@return previous org_value.
 *
 *@see
 */
G_API cbus_atomic_int cbus_atomic_or(cbus_atomic_int *org_value, cbus_atomic_int value);

/**
 *
 *@brief  atomic 'or' operation
 *
 *@param  org_value [io] origin value.
 *
 *@return previous org_value.
 *
 *@see
 */
G_API cbus_atomic_int cbus_atomic_and(cbus_atomic_int *org_value, cbus_atomic_int value);

/**
 *
 *@brief  compare org_value with old_value, if equal then org_value with new_value
 *
 *@param  org_value  [io] origin value.
 *        old_value [in] old value.
 *        new_value [in] new value.
 *
 *@return This returns GTRUE if the new value was successfully stored, 
 *        which will only happen when *org_value == old_value.
 *
 *@see
 */
G_API GBOL cbus_atomic_compare_exchange(cbus_atomic_int *org_value, cbus_atomic_int old_value, cbus_atomic_int new_value);

//////////////////////////////////////////////////////////////////////////
/**
 *
 *@brief  add a value to org_value as atomic operation(org_value is 64 bit)
 *
 *@param  org_value [io] origin value.
 *@param  add_value [in] add value.
 *
 *@return *org_value + add_value.
 *
 *@see    
 */
G_API cbus_atomic_int64 cbus_atomic_exchange_add64(cbus_atomic_int64 *org_value, cbus_atomic_int64 add_value);

/**
 *
 *@brief  add 1 to org_value as atomic operation(org_value is 64 bit)
 *
 *@param  org_value [io] origin value.
 *
 *@return previous org_value.
 *
 *@see
 */
G_API cbus_atomic_int64 cbus_atomic_increment64(cbus_atomic_int64 *org_value);

/**
 *
 *@brief  dec  1 from org_value as atomic operation(org_value is 64 bit)
 *
 *@param  org_value [io] origin value.
 *
 *@return previous org_value.
 *
 *@see
 */
G_API cbus_atomic_int64 cbus_atomic_decrement64(cbus_atomic_int64 *org_value);


G_C_HEADER_END


#endif //end of __CBUS_ATOMIC_H__
